/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action.wadministration;

import static com.opensymphony.xwork2.Action.INPUT;
import static com.opensymphony.xwork2.Action.SUCCESS;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.FieldExpressionValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.StringLengthFieldValidator;
import database.StaffDAO;
import java.util.Map;
import model.Customer;
import model.Memberaccount;
import model.Staff;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.validation.SkipValidation;

/**
 *
 * @author Trang
 */
@Namespace("/Administration/Member")
public class ChangePasswordAction extends ActionSupport{
    private String oldPassword;
    private String password;
    private String confirmPassword;
    public StaffDAO staffDAO = new StaffDAO();

    public String getOldPassword() {
        return oldPassword;
    }

    public void setOldPassword(String oldPassword) {
        this.oldPassword = oldPassword;
    }

    public String getPassword() {
        return password;
    }

     @RequiredStringValidator(message = "Password is required.")
    @StringLengthFieldValidator(message = "Password should contains 6-30 characters.", minLength = "6", maxLength = "30")
    public void setPassword(String password) {
        this.password = password;
    }

    public String getConfirmPassword() {
        return confirmPassword;
    }

     @FieldExpressionValidator(expression = "confirmPassword==password", message = "Passwords do not match.")
    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }
     
     @Action(value = "editPasswordForm", results = {
        @Result(name = "success", location = "/manageMember/w_staffEditPassword.jsp")
    })
    @SkipValidation
    public String viewPassword() {
        return SUCCESS;
    }
    @Override
    @Action(value = "editPassword", results = {
        @Result(name = "success", location = "/manageMember/w_staffEditSuccess.jsp"),
        @Result(name = "input", location = "/manageMember/w_staffEditPassword.jsp")
    })
    public String execute() throws Exception {
        Map session = ActionContext.getContext().getSession();
        Staff staff = ((Staff) session.get("staff"));
        
        if (oldPassword.equals(staff.getPassword())) {
            staff.setPassword(password);
            
            if (staffDAO.updatePasswordStaff(staff)) {
                addActionMessage("Your Password has been changed successfully. You must logout to see the change");
                
//                Customer customer = adao.getCustomerFromUsername((String) session.get("username"));
//                session.put("currentCustomer", customer);
                return SUCCESS;
            } else {
                addActionError("Cannot Update Password. Please try again later.");
                return INPUT;
            }
        } else {
            addActionError("Wrong Old Password");
            
            return INPUT;
        }
    }
}
